package controllers;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import models.Operator;
import services.AuthorityService;
import services.BackCountService;
import utils.BizConstants;
import vos.BackCountSearchVo;
import vos.BackCountVo;
import vos.ReturnCode;
import vos.ReturnListVo;

/**
 * 引用统计返款控制类
 * 
 * @ClassName: BackByCountController 
 * @author: wangsm
 * @date: 2016年7月14日 上午11:11:07
 */
public class BackByCountController extends BaseController {
	/**
	 * 引用统计返款
	 *
	 * @Title: backbycount 
	 * @return: void
	 */
	public static void backbycount(String menuid){
		Operator oper=getLoginUser();
		List<String> auths=AuthorityService.getAuthorityByOperAndMenu(menuid,oper.operatorid);
		for(String auth : auths){
			renderArgs.put(auth, auth);
		}
		render();
	}
	/**
	 * 获取统计列表
	 *
	 * @Title: getCount 
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @param countbegintime 统计开始时间
	 * @param countendtime 统计结束时间
	 * @param dealerid 经销商id
	 * @param dealername 经销商名称
	 * @param isreject 是否驳回
	 * @param shopid 门店id
	 * @param shopname 门店名称
	 * @return: void
	 */
	public static void getCount(int page,int rows, String sort,String order,
			String countbegintime,String countendtime,
			String dealerid,String dealername,String shopid,String shopname){
		if(page==0){
			page=1;
		}
		if(rows==0){
			rows=BizConstants.pagesize;
		}
		BackCountSearchVo vo=new BackCountSearchVo(page,rows,sort,order,
				countbegintime,countendtime,dealerid,dealername,shopid,shopname);
		long total=BackCountService.countByBack(vo);
		ReturnListVo listVo=new ReturnListVo();
		listVo.setTotal(total);
		List<BackCountVo> backvos=BackCountService.getCountByBack(vo);
		listVo.setRows(backvos);
		renderJSON(listVo);
	}
	/**
	 * 批量驳回
	 *
	 * @Title: betchReject 
	 * @param ids
	 * @return: void
	 */
	public static void betchReject(String ids,String rejreason){
		if(!checkAuthority("BACKBYCOUNT_BETCHREJECT")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		if(StringUtils.isEmpty(ids)){
			renderFailedJson(ReturnCode.ErrorCode, "选中记录为空");
		}
		if(StringUtils.isEmpty(rejreason)){
			renderFailedJson(ReturnCode.ErrorCode, "驳回信息不能为空");
		}
		String[] id=ids.split(",");
		List<String> listid=new ArrayList<String>();
		Collections.addAll(listid, id);
		List<BackCountVo> lists=BackCountService.getByIds(listid);
		if(lists==null||lists.size()<=0){
			renderFailedJson(ReturnCode.ErrorCode,"记录信息不存在");
		}
		for(BackCountVo vo :lists){
			if(vo.isback){
				renderFailedJson(ReturnCode.ErrorCode,"记录信息已返款");
			}
			if(vo.isreject){
				renderFailedJson(ReturnCode.ErrorCode,"记录信息已驳回");
			}
			if(!vo.issave){
				renderFailedJson(ReturnCode.ErrorCode,"记录信息未保存");
			}
		}
		Operator oper=getLoginUser();
		boolean issuccess=BackCountService.betchReject(listid,oper.operatorid,rejreason);
		if(issuccess){
			renderFailedJson(ReturnCode.SuccessCode,"驳回成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode,"驳回失败");
		}
	}
	/**
	 * 驳回
	 *
	 * @Title: reject 
	 * @param id 记录id
	 * @param rejreason 驳回原因
	 * @return: void
	 */
	public static void reject(Long id,String rejreason){
		if(!checkAuthority("BACKBYCOUNT_REJECT")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		if(id==null){
			renderFailedJson(ReturnCode.ErrorCode, "选中的记录不能为空");
		}
		if(StringUtils.isEmpty(rejreason)){
			renderFailedJson(ReturnCode.ErrorCode, "驳回原因不能为空");
		}
		BackCountVo back=BackCountService.getById(id);
		if(back==null){
			renderFailedJson(ReturnCode.ErrorCode, "记录不存在");
		}
		if(back.isback){
			renderFailedJson(ReturnCode.ErrorCode, "该记录已返款");
		}
		if(back.isreject){
			renderFailedJson(ReturnCode.ErrorCode, "该记录已驳回");
		}
		if(!back.issave){
			renderFailedJson(ReturnCode.ErrorCode, "该记录未保存");
		}
		Operator oper=getLoginUser();
		boolean issuccess=BackCountService.reject(id,oper.operatorid,rejreason);
		if(issuccess){
			renderFailedJson(ReturnCode.SuccessCode,"驳回成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode,"驳回失败");
		}
	}
	/**
	 * 详情
	 *
	 * @Title: viewCount 
	 * @return: void
	 */
	public static void countView(Long id){
		if(id==null){
			render("errors/404.html");
		}
		if(!checkAuthority("BACKBYCOUNT_VIEW")){
			render("errors/404.html");
		}
		BackCountVo back=BackCountService.getById(id);
		renderArgs.put("count", back);
		render();
	}
}
